package src.percolation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import org.apache.commons.math.stat.regression.SimpleRegression;

public class AnalizeRegressionResults {

	public static final String dataDir = "data";
	public static final String output = "regResults.dat";
	
	public static void main(String[] argvs) throws IOException{
		
		File files = new File(dataDir); //need a data directory at this path
		String[] list = files.list();
		BufferedReader input = null;
		
		//to compute results on different regressions
		File outputFile = new File(output);
		FileOutputStream fos = new FileOutputStream(outputFile); 
		
		for(int i=0;i<list.length;i++){
			
			//to read the file
			input = new BufferedReader(new FileReader(new File(dataDir,list[i])));
			String line = null;
			
			//to process the data
			SimpleRegression regression = new SimpleRegression();
			
			 while (( line = input.readLine()) != null){
				 String[] dataLine = line.split("\\,");
				 double x =(new Double(dataLine[0])).doubleValue();
				 double y =(new Double(dataLine[1])).doubleValue();
				 //if(condition)
				 regression.addData(x,y);
			 }
			 double chi = regression.getSumSquaredErrors();
			 fos.write((list[i]+","+chi+"/n").getBytes());
			 fos.flush();
		}
		fos.close();
	}
	
	
}
